import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import http from '@/mixins/http.js'
const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync("token") || "",
		isLogin: uni.getStorageSync("isLogin") || false,
		userInfo: uni.getStorageSync("userInfo") || {},
		addressText: uni.getStorageSync("addressText") || "定位中",
		merchant: uni.getStorageSync("merchant") || {}
	},
	getters: {
		token: state => state.token,
		isLogin: state => state.isLogin,
		userInfo: state => state.userInfo,
		addressText: state => state.addressText,
		merchant: state => state.merchant
	},
	mutations: {
		setMerchant(state, payload) {
			state.merchant = payload;
			uni.setStorageSync("merchant", payload);
		},
		// 存取token
		seToken(state, payload) {
			state.token = payload;
			uni.setStorageSync("token", payload);
		},
		// 登录态
		isLogin(state, data) {
			state.isLogin = data;
			
			uni.setStorageSync('isLogin', data);
		},
		// 用户信息
		setUserInfo(state, data) {
			state.userInfo = data;
			uni.setStorageSync("userInfo", data);
		},
		setAddressText(state, data) {
			state.addressText = data;
			uni.setStorageSync("addressText", data);
		}
	},
	actions: {
		// 获取个人信息
		getUserInfo({
			commit,
			dispatch,
			getters,
			state
		}, token = '') {
			return new Promise((resolve, reject) => {
				token && uni.setStorageSync('token', token);
				http.post('wanlshop/user/refresh').then(res => {
					if (res.code === 1) {
						commit('setUserInfo', res.data);
						commit('isLogin', true);
						resolve(res.data)
					}
				})
			})
		},
		// 退出登录
		logout({
			commit
		}) {
			commit('seToken', "");
			commit('isLogin', false);
			commit('setUserInfo', {});
		},
	}
})
export default store
